package com.gtm.service;

import com.gtm.model.Assignment;
import com.gtm.model.Topic;
import com.gtm.model.User;
import com.gtm.repository.AssignmentRepository;
import com.gtm.repository.TopicRepository;
import com.gtm.repository.UserRepository;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by 骆清波 on 2017/11/16.
 */
@Service
public class AssignmentService extends BaseService<Assignment>{
    @Autowired
    AssignmentRepository asnr;

    @Autowired
    UserRepository usr;

    public Assignment submitWord(String notes, String teacherId, String teacherName, String topicId, String topicName, MultipartFile word) throws IOException {
            Assignment as=asnr.findByTopicId(topicId);
//            Calendar rightNow = Calendar.getInstance();
//            rightNow.setTime(new Date());
//            rightNow.add(Calendar.HOUR_OF_DAY, 8);
            Date createtime = new Date();
            if(as==null) {
                Assignment assignment = new Assignment();
                BasicDBObject task = new BasicDBObject();
                List<DBObject> list = new ArrayList<DBObject>();
                String wordId = upUserFile(word, assignment);
                task.put("wordId", wordId);
                task.put("notes", notes);
                task.put("createtime", createtime);
                list.add(task);
                assignment.setTaskList(list);
                assignment.setTeacherId(teacherId);
                assignment.setTeacherName(teacherName);
                assignment.setTopicId(topicId);
                assignment.setTopicName(topicName);

                return asnr.save(assignment);
            }else{
                BasicDBObject task = new BasicDBObject();
                List<DBObject> list = as.getTaskList();
                String wordId = upUserFile(word, as);
                task.put("wordId", wordId);
                task.put("notes", notes);
                task.put("createtime", createtime);
                list.add(task);
                as.setTaskList(list);
                return asnr.save(as);
            }


    }

    public Assignment find(String topicId) {
        return asnr.findByTopicId(topicId);
    }

    public Assignment submitStuWord(String notes, String username, String realname, String topicId, MultipartFile word) throws IOException {
        Assignment as=asnr.findByTopicId(topicId);
//        Calendar rightNow = Calendar.getInstance();
//        rightNow.setTime(new Date());
//        rightNow.add(Calendar.HOUR_OF_DAY, 8);
        if(as==null)
            as=new Assignment();
        Date createtime = new Date();
        BasicDBObject stu = new BasicDBObject();
        if(as.getStuList()==null) {
            List<DBObject> list = new ArrayList<DBObject>();
            String wordId = upUserFile(word, as);
            stu.put("wordId",wordId);
            stu.put("username", username);
            stu.put("notes", notes);
            stu.put("realname", realname);
            stu.put("createtime",createtime);
            list.add(stu);
            as.setStuList(list);
            User user=usr.findByUsername(username);
            System.out.println(user.getDetail());
            user.setAdditionalData("state","TASK_PENDING");
            user.setAdditionalData("taskId",asnr.save(as).getId());
            usr.save(user);
            return null;
        }else {
            List<DBObject> list=as.getStuList();
            String wordId = upUserFile(word, as);
            stu.put("wordId",wordId);
            stu.put("username", username);
            stu.put("notes", notes);
            stu.put("realname", realname);
            stu.put("createtime",createtime);
            list.add(stu);
            as.setStuList(list);
            User user=usr.findByUsername(username);
            System.out.println(user.getDetail());
            user.setAdditionalData("state","TASK_PENDING");
            user.setAdditionalData("taskId",asnr.save(as).getId());
            usr.save(user);
            return null;
        }


    }


    public Assignment passAssignment(String username) {
        User user=usr.findByUsername(username);
        user.setAdditionalData("state","ASSIGNMENT_PASS");
        usr.save(user);
        return null;
    }

    public Assignment depassAssignment(String username) {
        User user=usr.findByUsername(username);
        user.setAdditionalData("state","");
        usr.save(user);
        return null;
    }
}
